import { getLocation } from '../../wxapi/loacation/home.js'
import { mySetStore } from '../../wxapi/store/index.js'
import { getUserCity , getDoubanData } from '../../network/home.js'



Page({
  data:{
    movies:{},
    top250Record:{start:0,flag:true}
  },
  onLoad(){
    // 同步读取缓存数据
    this.data.movies = wx.getStorageSync('movies')||{}
    this.setData(this.data)
    // 加载数据
    this.getLocationData()
    
  },
  // 获取用户地理位置并且拉去相关信息
  getLocationData(){

    getLocation()
      .then(res => {
        // 获取经纬度
        const coordinate = `${res.latitude},${res.longitude}`
        return getUserCity(coordinate)
      })
      .then(res => {
        // 通过逆地理编码获取所在城市
        let city = res.data.result.addressComponent.city
        console.log(city)
        let paramInTheaters = {
          url: '/v2/movie/in_theaters',
          city: city.slice(0, city.length - 1)
        }
        let paramNewMovies = {
          url: '/v2/movie/new_movies'
        }
        let paramComingSoon = {
          url: '/v2/movie/coming_soon'
        }  
        return Promise.all([
          getDoubanData(paramInTheaters),
          getDoubanData(paramNewMovies),
          getDoubanData(paramComingSoon),
          this.getTop250Data(),
        ])
      })
      .then(res => {
        // 获取豆瓣返回数据
        // console.log(res.data)
        // console.log(res.data.subjects[0].title)
        // console.log(res.data.subjects[0].images.small)
        // console.log(res.data.subjects[0].rating.average)
       
        res[0].data.headerTitle = '影院热映'
        res[0].data.path = '/v2/movie/in_theaters'
        res[1].data.headerTitle = '豆瓣新片'
        res[1].data.path = '/v2/movie/new_movies'
        res[2].data.headerTitle = '即将上映'
        res[2].data.path = '/v2/movie/coming_soon'
        res[3].data.headerTitle = 'Top250'
        res[3].data.path = '/v2/movie/top250'
        this.setData({
          'movies.inTheatersData': res[0].data,
          'movies.newMoviesData': res[1].data,
          'movies.comingSoon': res[2].data,
          'movies.top250': res[3].data,
        })
        // 缓存movies
        this.cacheData()
        

      })
      .catch(res => {
        console.log(res)
        wx.s_douban.toast('获取位置失败')
      })
  },
  // 搜索按钮跳转
  handleSearchBtn(){
    wx.navigateTo({
      url: '/pages/searchpage/searchpage',
    })
  },
  //缓存movies数据
  cacheData(){
    mySetStore({
      key: 'movies',
      data: this.data.movies
    })
      .then(() => {
        console.log('缓存成功')
      }, () => {
        console.log('缓存失败')
      })
  },
  // top250
  getTop250Data(){
    let option = {
      url: '/v2/movie/top250',
      start: this.data.top250Record.start,
      count: 20,
    }
    this.data.top250Record.start +=20
    return getDoubanData(option)
  },
  handleScrollToLower(event){
    if (event.detail.title === 'Top250'){
      // 拉低加载更多
      if (this.data.top250Record.flag) {
        this.data.top250Record.flag = false
        this.getTop250Data()
          .then(res => {
            console.log(11)
            this.data.top250Record.flag = true
            let newList = [...this.data.movies.top250.subjects, ...res.data.subjects]
            this.setData({
              'movies.top250.subjects': newList
            })

          })
      }
    }

  }
})